package cn.itcast.handler;

import cn.itcast.server.session.SessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @Name: QuitHandler
 * @Description:
 * @Author: Mr.Tong
 */
@Slf4j
@ChannelHandler.Sharable
public class QuitHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //正常退出的情况
        SessionFactory.getSession().unbind(ctx.channel());//解绑
        log.debug("{} 已经断开", ctx.channel());
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //异常退出的情况
        SessionFactory.getSession().unbind(ctx.channel());//解绑
        log.debug("{} 已经异常断开-异常信息{}", ctx.channel(), cause.getMessage());
    }
}
